/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.sharedbills.server.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.OneToMany;

import org.jb.sharedbills.shared.model.Event;

import com.google.appengine.api.datastore.Key;

@Entity
public class EventAccess implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Lob
	private Event event;
	
	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Key key;
	
	private String creator;
	
	private String creatorEmail;

	private String description;
	
	@OneToMany(mappedBy = "event", cascade = CascadeType.ALL)
	private List<EventMessage> messages = new ArrayList<EventMessage>();

	/**
	 * @param event the event to set
	 */
	public void setEvent(Event event) {
		this.event = event;
	}

	/**
	 * @return the event
	 */
	public Event getEvent() {
		return event;
	}

	/**
	 * @param key the key to set
	 */
	public void setKey(Key key) {
		this.key = key;
	}

	/**
	 * @return the key
	 */
	public Key getKey() {
		return key;
	}

	/**
	 * 
	 * @param userID
	 */
	public void setCreator(String userID) {
		creator = userID;
	}
	
	/**
	 * 
	 * @return
	 */
	public String getCreator() {
		return creator;
	}

	/**
	 * 
	 * @param userID
	 * @return
	 */
	public boolean canWrite(String userID) {
		return userID.equals( creator );
	}

	/**
	 * 
	 * @return
	 */
	public String getDescription() {
		return description;
	}
	
	/**
	 * 
	 * @param description
	 */
	public void setDescription( String description ) {
		this.description = description;
	}

	/**
	 * @return the messages
	 */
	public List<EventMessage> getMessages() {
		return messages;
	}

	/**
	 * @param creatorEmail the creatorEmail to set
	 */
	public void setCreatorEmail(String creatorEmail) {
		this.creatorEmail = creatorEmail;
	}

	/**
	 * @return the creatorEmail
	 */
	public String getCreatorEmail() {
		return creatorEmail;
	}
}
